#include<stdio.h>
#include<stdlib.h>
#include<string.h>




typedef struct student
{
    char name[128];
    char sex[50];
    int age;
    int soc;
}student;

student mess[50] = 
{
    {.name = "石昊", .sex="男", .age = 18, .soc = 96},
    {.name = "磊哥", .sex="男", .age = 19, .soc = 99},
    {.name = "狠人", .sex="男", .age = 20, .soc = 98},
    {.name = "萧炎", .sex="男", .age = 21, .soc = 97},
};

student * myrealloc(student *D,size_t newsize,size_t oldsize);


int main(int argc, char const *argv[])
{
    size_t newsize;
    student *D = (student *)malloc(sizeof(mess));

    for (size_t i = 0; i < 4; i++)
    {
        D[i] = mess[i];
    }
    

    printf("请输入新的空间大小：");
    scanf("%ld",&newsize);

    size_t oldsize = sizeof(*D);
    myrealloc(D,newsize,oldsize);
    return 0;
}

student * myrealloc(student *D,size_t newsize,size_t oldsize)
{

    //原来的旧空间为空
    if(D == NULL)
    {
        D = malloc(sizeof(newsize));
        return D;
    }
    //新空间为0
    if(0 == newsize)
    {
        free(D);
        return D;
    }
    // //创建新的空间
    student * D1 = (student * )malloc(newsize * sizeof(student));

    if(D == NULL)
    {
        printf("创建内存失败\n");
        return NULL;
    }

    for(size_t j = 0;j < oldsize;j++)
    {
        D1[j] = D[j];
    }

    for (size_t i = 0; i < newsize; i++)
    {
        printf("姓名：%s\t性别:%s\t年龄:%d\t成绩:%d\n"
            ,D1[i].name,D1[i].sex,D1[i].age,D1[i].soc);
    }
    
    free(D);

    return D1;
}